package MyProject;

import java.util.ArrayList;
import java.util.Random;

public class MyThread extends Thread{

    private final ArrayList<Integer> money;

    private int sum = 0;
    public MyThread(ArrayList<Integer> money,String s) {
        super(s);
        this.money = money;

    }

    @Override
    public void run() {
        String name = Thread.currentThread().getName();
        Random random = new Random();

        while (true){
            synchronized (money){
                if (money.size() == 0){
                    break;
                }

                //随机生成数组以内的索引
                int index = random.nextInt(money.size());
                int m = money.remove(index);
                System.out.println(m + " 这次抢到了: "+m+" 块");


                sum += m;

                try {
                    Thread.sleep(20);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }

            }
        }

    }
    public int getSum(){
        return sum;
    }

}
